//
//  ZCShowBigPictureViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/31.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCShowBigPictureViewController.h"
#import "ZCEssenceModel.h"

@interface ZCShowBigPictureViewController ()

@property (weak, nonatomic) UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end

@implementation ZCShowBigPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self setupImageView];
    [self setupUI];
}

-(void)setupUI{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(gobackBtnAction)];
    
    [self.view addGestureRecognizer:tapGesture];
    
    [UIApplication sharedApplication].statusBarStyle  = YES;
    
    
    //返回按钮
    UIButton *goBackBtn = [UIButton buttonWithBackgroundImage:@"show_image_back_icon"];
    [self.view addSubview:goBackBtn];
    [goBackBtn addTarget:self action:@selector(gobackBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [goBackBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(30);
        make.leading.equalTo(self.view).offset(10);
    }];
    
    //转发按钮
    UIButton *repostBtn = [UIButton buttonWithTitle:@"转发" textColor:[UIColor blackColor] selectedColor:nil fontSize:15];
    [self.view addSubview:repostBtn];
    [repostBtn addTarget:self action:@selector(repostBtnAction) forControlEvents:UIControlEventTouchUpInside];
    repostBtn.backgroundColor = [UIColor colorWithRed:233 green:233 blue:233 alpha:0.5];
    
    //保存按钮
    UIButton *saveBtn = [UIButton buttonWithTitle:@"保存" textColor:[UIColor blackColor] selectedColor:nil fontSize:15];
    [self.view addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.backgroundColor = [UIColor colorWithRed:233 green:233 blue:233 alpha:0.5];

    
    [repostBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-20);
        make.trailing.equalTo(self.view).offset(-10);
        make.size.mas_equalTo(CGSizeMake(50, 30));
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(repostBtn);
        make.trailing.equalTo(repostBtn.mas_leading).offset(-10);
        make.size.equalTo(repostBtn);
    }];
}


-(void)setupImageView{
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView = scrollView;
    scrollView.bounces = NO;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    [scrollView addSubview:imageView];
    imageView.backgroundColor = ZCGlobalRBG;
    self.imageView = imageView;
    
    imageView.center = scrollView.center;
    imageView.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 10);
    

}


-(void)setModel:(ZCEssenceModel *)model{
    _model = model;
    
    CGFloat imageHeight = (model.height.floatValue / model.width.floatValue) * self.view.width;
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    ZCLog(@"%f",imageHeight);
    if (imageHeight <= screenSize.height) {
        self.scrollView.contentSize = CGSizeMake(screenSize.width, screenSize.height);
        
        self.imageView.center = self.scrollView.center;
        self.imageView.bounds = CGRectMake(0, 0, screenSize.width, imageHeight);

    }else{
        
        self.scrollView.contentSize = CGSizeMake(screenSize.width, imageHeight);
        self.imageView.frame = CGRectMake(0, 0, screenSize.width, imageHeight);
    }
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image2] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        ZCLog(@"%.0f %%",1.0*receivedSize/expectedSize *100);
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
}



-(void)gobackBtnAction{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)repostBtnAction{
    ZCLog(@"转发");
    [SVProgressHUD showSuccessWithStatus:@"转发成功"];

}
-(void)saveBtnAction{
    
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败"];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"保存成功"];
    }
}
@end
